<?php
// ## Archive Selectbox##
function showArchiveList($parameter) {
	global $blog, $database, $blogid, $defaultURL, $service, $suri, $configVal;
	if (!isset($blog['blogLanguage'])) {
		$blog['blogLanguage'] = $service['language'];
	}
	switch($blog['blogLanguage']) {
		case "zh-TW":
			$previewMsg = "會顯示成下拉式清單。";
			$dateline = array(' 年 ',' 月');
		break;
		case "zh-CN":
			$previewMsg = "会显示成下拉式列表。";
			$dateline = array(' 年 ',' 月');
		break;
		default:
			$previewMsg = "글 보관 목록을 셀렉트박스 형태로 출력해서 보여줍니다.";
			$dateline = array(' 년 ',' 월');
	}
	if (isset($parameter['preview'])) {
		// preview mode
		return $previewMsg;
	}
	requireComponent('Textcube.Function.misc');
	$data = misc::fetchConfigVal($configVal);
	$data['datestyle']		= isset($data['datestyle'])?$data['datestyle']:1; 
	$data['archiveheader']	= isset($data['archiveheader'])?$data['archiveheader']:"<div class=\"listbox\">\n<h3>archive</h3>".CRLF;
	$data['archivefooter']	= isset($data['archivefooter'])?$data['archivefooter']:"</div>".CRLF;
	
	$visibility = doesHaveOwnership()?'':' AND visibility > 0 ';
	$years	= DBQuery::queryAll("SELECT YEAR(FROM_UNIXTIME(published)) as  period, COUNT(*) count FROM {$database['prefix']}Entries WHERE blogid = $blogid AND draft = 0 {$visibility} AND category >= 0 GROUP BY period ORDER BY period DESC"); 
	$row = DBQuery::queryAll("SELECT EXTRACT(year_month FROM FROM_UNIXTIME(published)) period, COUNT(*) count FROM {$database['prefix']}Entries WHERE blogid = $blogid AND draft = 0 {$visibility} AND category >= 0 GROUP BY period ORDER BY period DESC");
	$target = $data['archiveheader'];
	$target .= "<div id=\"archivebox\">".CRLF;
	$target .= "<select id=\"archiveSelect\" onchange=\"Go_URL(this);\">".CRLF;
	$target .= "<option value=\"\">---------- Select Archive ----------</option>".CRLF;
	foreach($years as $items){
		$selectedYY = ($suri['value'] == $items['period'])?" selected":"";
		$target .= "<option class=\"archiveYear\" value=\"{$defaultURL}/archive/{$items['period']}\" {$selectedYY}>&nbsp;{$items['period']} ({$items['count']})</option>".CRLF;
		foreach($row as $item){
			if($items['period'] == substr($item['period'],0,4)){
				$tempYear = substr($item['period'],0,4);
				switch($data['datestyle']){
					case 1:
						$a_date = substr($item['period'],0,4).'/'.substr($item['period'],4); break;
					case 2:
						$a_date = substr($item['period'],0,4).$dateline[0].substr($item['period'],4).$dateline[1]; break;
					case 3:
						$a_date = date("F Y",(mktime(0,0,0,substr($item['period'],4),1,substr($item['period'],0,4)))); break;
					default:
						$a_date = substr($item['period'],0,4).'/'.substr($item['period'],4); break;
				}
				$selectedMM = ($suri['value'] == $item['period'])?" selected":"";
				$target .= "<option class=\"archiveMonth\" value=\"{$defaultURL}/archive/{$item['period']}\" {$selectedMM}>&nbsp;&nbsp;&nbsp;{$a_date}({$item['count']})</option>".CRLF;
			}
		}
	}
	$target .= "</select>".CRLF;
	$target .= "</div>".CRLF;
	$target .= $data['archivefooter'];
	return $target;	
}
function showArchiveScriptStyle($target){
	global $blogURL;
	$style  = "<script type=\"text/javascript\" src=\"".$blogURL."/plugin/archiveScriptStyle/?mode=script\"></script>".CRLF;
	$style .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"".$blogURL."/plugin/archiveScriptStyle/?mode=style\" />".CRLF;
	$target .= $style;
	return $target;
}

function getArchiveScriptStyle($target){
	global $configVal;
	requireComponent('Textcube.Function.misc');
	$data = misc::fetchConfigVal($configVal);
	$data['archivewidth']  = isset($data['archivewidth'])?$data['archivewidth']:165;
	$data['archivemargin'] = isset($data['archivemargin'])?$data['archivemargin']:"5px 0px 0px 3px";
	if($_GET['mode'] == "style"){
		header('Content-type: text/css');
		echo '
			/* Archive to Selectbox CSS Setting */
			#archivebox{margin:'.$data['archivemargin'].';height:20px;}
			#archiveSelect{font:8pt tahoma; width:'.$data['archivewidth'].'px; color:#555;}
			#archiveSelect option{background-color: #ffffff;}
			#archiveSelect .archiveYear{background-color: #eee;color:#222;}
			#archiveSelect .archiveMonth{background-color: #fff;}
		';
	}else if($_GET['mode'] == "script"){
		header('Content-type: text/javascript');
		echo '
			/* Archive to Selectbox Script */
			function Go_URL(selectObject){if(selectObject.options[selectObject.selectedIndex].value){location.href=selectObject.options[selectObject.selectedIndex].value;}}
		';
	}
	flush();
}

function ArchiveDateStyleDataSet($DATA){
	requireComponent('Textcube.Function.misc');
	$cfg = misc::fetchConfigVal($DATA);
	return true;
}
?>